package com.lframework.xingyun.basedata.bo.product.costcard;

import cn.hutool.core.collection.CollUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.lframework.xingyun.basedata.entity.Product;
import com.lframework.xingyun.basedata.entity.ProductBrand;
import com.lframework.xingyun.basedata.entity.ProductCategory;
import com.lframework.xingyun.basedata.entity.ProductCostCardDetail;
import com.lframework.xingyun.basedata.service.product.ProductBrandService;
import com.lframework.xingyun.basedata.service.product.ProductCategoryService;
import com.lframework.xingyun.basedata.service.product.ProductCostCardDetailService;
import com.lframework.xingyun.basedata.service.product.ProductService;
import io.swagger.annotations.ApiModelProperty;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Data;

/**
 * 商品成本卡明细
 *
 * @author zmj
 * @since 2024-01-01
 */
@Data
public class ProductCostCardDetailBo extends BaseBo<ProductCostCardDetail> {

  /**
   * ID
   */
  @ApiModelProperty("ID")
  private String id;

  /**
   * 成本卡ID
   */
  @ApiModelProperty("成本卡ID")
  private String costCardId;

  /**
   * 源成分商品ID
   */
  @ApiModelProperty("源成分商品ID")
  private String sourceProductId;

  /**
   * 成分商品ID
   */
  @ApiModelProperty("成分商品ID")
  private String ingredientProductId;

  /**
   * 成分商品编号
   */
  @ApiModelProperty("成分商品编号")
  private String ingredientProductCode;

  /**
   * 成分商品名称
   */
  @ApiModelProperty("成分商品名称")
  private String ingredientProductName;

  /**
   * 成分商品分类名称
   */
  @ApiModelProperty("成分商品分类名称")
  private String ingredientCategoryName;

  /**
   * 成分商品品牌名称
   */
  @ApiModelProperty("成分商品品牌名称")
  private String ingredientBrandName;

  /**
   * 成分商品单位
   */
  @ApiModelProperty("成分商品单位")
  private String ingredientUnit;

  /**
   * 消耗单位ID
   */
  @ApiModelProperty("消耗单位ID")
  private String unitId;

  /**
   * 消耗数量
   */
  @ApiModelProperty("消耗数量")
  private Integer consumeQuantity;

  /**
   * 类型 0=主成分 1=替换品
   */
  @ApiModelProperty("类型 0=主成分 1=替换品")
  private Integer type;

  /**
   * 单位成本
   */
  @ApiModelProperty("单位成本")
  private BigDecimal unitCost;

  /**
   * 总成本
   */
  @ApiModelProperty("总成本")
  private BigDecimal totalCost;

  /**
   * 排序号
   */
  @ApiModelProperty("排序号")
  private Integer orderNo;

  /**
   * 备注
   */
  @ApiModelProperty("备注")
  private String description;

  @ApiModelProperty("替换品")
  private List<ProductCostCardDetailBo> replacements;

  public ProductCostCardDetailBo() {

  }

  public ProductCostCardDetailBo(ProductCostCardDetail dto) {

    super(dto);
  }

  @Override
  protected void afterInit(ProductCostCardDetail dto) {

    // 查询成分商品信息
    ProductService productService = ApplicationUtil.getBean(ProductService.class);
    Product product = productService.findById(dto.getIngredientProductId());

    ProductCategoryService productCategoryService = ApplicationUtil.getBean(
        ProductCategoryService.class);
    ProductCategory productCategory = productCategoryService.findById(product.getCategoryId());

    ProductBrandService productBrandService = ApplicationUtil.getBean(ProductBrandService.class);
    ProductBrand productBrand = productBrandService.findById(product.getBrandId());

    this.ingredientProductCode = product.getCode();
    this.ingredientProductName = product.getName();
    this.ingredientCategoryName = productCategory.getName();
    this.ingredientBrandName = productBrand.getName();
    this.ingredientUnit = product.getUnit();

    if (this.type == 0) {
        ProductCostCardDetailService productCostCardDetailService = ApplicationUtil.getBean(
                ProductCostCardDetailService.class);
        List<ProductCostCardDetail> replacementsByCostCardId = productCostCardDetailService.getReplacementsByCostCardId(this.ingredientProductId);
        if (CollUtil.isNotEmpty(replacementsByCostCardId)) {
            this.replacements = replacementsByCostCardId.stream().map(ProductCostCardDetailBo::new).collect(Collectors.toList());
        }
    }
  }
} 